package cn.iocoder.yudao.module.digital.service.patent;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.digital.controller.admin.patent.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.patent.PatentDO;
import cn.iocoder.yudao.module.patent.dal.dataobject.authors.AuthorsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.digital.dal.mysql.patent.PatentMapper;
import cn.iocoder.yudao.module.patent.dal.mysql.authors.AuthorsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.digital.enums.ErrorCodeConstants.*;

/**
 * 专利信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class PatentServiceImpl implements PatentService {

    @Resource
    private PatentMapper patentMapper;
    @Resource
    private AuthorsMapper authorsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createPatent(PatentSaveReqVO createReqVO) {
        // 插入
        PatentDO patent = BeanUtils.toBean(createReqVO, PatentDO.class);
        patentMapper.insert(patent);

        // 插入子表
        createAuthorsList(patent.getId(), createReqVO.getAuthorss());
        // 返回
        return patent.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePatent(PatentSaveReqVO updateReqVO) {
        // 校验存在
        validatePatentExists(updateReqVO.getId());
        // 更新
        PatentDO updateObj = BeanUtils.toBean(updateReqVO, PatentDO.class);
        patentMapper.updateById(updateObj);

        // 更新子表
        updateAuthorsList(updateReqVO.getId(), updateReqVO.getAuthorss());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePatent(Integer id) {
        // 校验存在
        validatePatentExists(id);
        // 删除
        patentMapper.deleteById(id);

        // 删除子表
        deleteAuthorsByPatentId(id);
    }

    private void validatePatentExists(Integer id) {
        if (patentMapper.selectById(id) == null) {
            throw exception(PATENT_NOT_EXISTS);
        }
    }

    @Override
    public PatentDO getPatent(Integer id) {
        return patentMapper.selectById(id);
    }

    @Override
    public PageResult<PatentDO> getPatentPage(PatentPageReqVO pageReqVO) {
        return patentMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（专利作者信息） ====================

    @Override
    public List<AuthorsDO> getAuthorsListByPatentId(Integer patentId) {
        return authorsMapper.selectListByPatentId(patentId);
    }

    private void createAuthorsList(Integer patentId, List<AuthorsDO> list) {
        list.forEach(o -> o.setPatentId(patentId));
        authorsMapper.insertBatch(list);
    }

    private void updateAuthorsList(Integer patentId, List<AuthorsDO> list) {
        deleteAuthorsByPatentId(patentId);
		list.forEach(o -> o.setPatentId(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createAuthorsList(patentId, list);
    }

    private void deleteAuthorsByPatentId(Integer patentId) {
        authorsMapper.deleteByPatentId(patentId);
    }

}